package labb2.del1;

import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

/**
 * This class represents a panel where one or many <code>Shape</code> objects
 * may be added. The <code>Shape</code> objects will have random start position,
 * color and size.
 *
 * @author Mikko Puustinen
 * @version 0.5, 2007-sep-30
 */
public class BouncePanel extends JPanel implements ActionListener {

    private int width = 800;
    private int height = 600;
    private javax.swing.Timer timer;
    private Random rand = new Random();
    private Shape[] shapes;

    /**
     * Creates a <code>BouncePanel</code>
     */
    public BouncePanel() {
        shapes = new Shape[30];
        Rectangle box = new Rectangle(width, height);
        for (int i = 0; i < shapes.length; i++) {
            int x = rand.nextInt(1 + i * 5);
            int y = rand.nextInt(i * x + 1);
            int wid = rand.nextInt(width / 5) + 5;
            int hei = rand.nextInt(height / 5) + 5;
            int col = rand.nextInt();

            if (i % 3 == 0) {
                shapes[i] = new Line(x, y, new Color(col), wid, hei);
                shapes[i].setVelocity(rand.nextInt(10) + 1, rand.nextInt(10) + 1);
            } else if (i % 3 == 1) {
                shapes[i] = new Circle(x, y, new Color(col), wid);
                shapes[i].setVelocity(rand.nextInt(10) + 1, rand.nextInt(10) + 1);
            } else {
                shapes[i] = new Rect(x, y, new Color(col), wid, hei);

            }
            shapes[i].setVelocity(rand.nextInt(10) + 1, rand.nextInt(10) + 1);
            shapes[i].setBoundingBox(box);
            if (shapes[i] instanceof FillableShape) {
                if (i % 2 == 0)
                    ((FillableShape) shapes[i]).toggleFilled();
            }
        }

        setPreferredSize(new Dimension(width, height));
        timer = new javax.swing.Timer(20, this);
        timer.start();
    }

    /**
     * Overrides the <code>paintComponent()</code> in javax.swing.JComponent.
     * Calls the <code>paint()</code> in every <code>Shape</code> object.
     *
     * @param g Graphics object.
     */
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        for (Shape shape : shapes)
            shape.paint(g);
    }

    /**
     * Performs a <code>move()</code> on every <code>Shape</code> object
     * everytime the <code>Timer</code> creates an <code>ActionEvent</code>
     * object.
     *
     * @param e the generated ActionEvent by the Timer.
     */
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == timer) {
            for (Shape shape : shapes) {
                shape.getBox().setSize(getWidth(), getHeight());
                shape.move();
            }
            repaint();
        }
    }
}